//
//  ExternalViewController.m
//  day-07
//
//  Created by hcios on 2017/7/9.
//  Copyright © 2017年 hcios. All rights reserved.
//

#import "ExternalViewController.h"
#import "AFNetworking.h"
#import "InternalModel.h"
#import "InternalCollectionViewCell.h"
#import "CardViewController.h"

#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
#define RGBA(r,g,b,a) [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a]
#define RGB(r,g,b) RGBA(r,g,b,1.0f)


@interface ExternalViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic,strong) InternalModel *model;
@property (nonatomic,strong) NSMutableArray *dataArr;
@property (nonatomic,strong) UICollectionView *cityCV;

@end

static NSString *const CollectionViewCellIdentifier = @"cellID";

@implementation ExternalViewController

-(instancetype)init{
    if (self = [super init]) {
        _dataArr = [[NSMutableArray alloc] init];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self initCity];
    [self netWorking];
}

-(void)initCity{
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.itemSize = CGSizeMake(170, 280);
    layout.minimumLineSpacing = 15;
    layout.minimumInteritemSpacing = 15;
    self.cityCV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64) collectionViewLayout:layout];
    self.cityCV.delegate = self;
    self.cityCV.dataSource = self;
    self.cityCV.showsVerticalScrollIndicator = NO;
    //注册cell
    [self.cityCV registerClass:[InternalCollectionViewCell class] forCellWithReuseIdentifier:@"cellID"];
    self.cityCV.backgroundColor = RGB(214, 226, 238);
    [self.view addSubview:self.cityCV];
    
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _dataArr.count;
}


- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    InternalCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CollectionViewCellIdentifier forIndexPath:indexPath];
    cell.model = _dataArr[indexPath.row];
    return cell;
}

#pragma mark - CollectionViewDelegate
//设置边距
- (UIEdgeInsets) collectionView:(UICollectionView *)collectionView
                         layout:(UICollectionViewLayout *)collectionViewLayout
         insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(15.0f, 10.0f, 0.0f, 10.0f);
}


//点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    NSLog(@"%ld%ld",(long)[indexPath section],(long)[indexPath row]);
    InternalModel * model = _dataArr[indexPath.row];
    CardViewController * VC = [[CardViewController alloc] init];
    VC.selfID = model.destinations_id;
    VC.cityName = model.destinations_name_zh_cn;
    [self.navigationController pushViewController:VC animated:YES];
    
}


-(void)netWorking{
    NSString *urlString = @"http://chanyouji.com/api/destinations.json";
    NSDictionary *param = @{@"page":@"1"};
    
    //网络请求
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:urlString parameters:param progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        for (NSDictionary *dict in responseObject) {
            NSLog(@"%@",dict[@"category"]);
            NSLog(@"%@",dict[@"destinations"]);
            
            if ([dict[@"category"] isEqualToString:@"99"] || [dict[@"category"] isEqualToString:@"999"]) {
                
                for (NSDictionary *dict2 in dict[@"destinations"]) {
                    NSLog(@"%@",dict2);
                    _model = [[InternalModel alloc] initWithDict:dict2];
                    [_dataArr addObject:_model];
                }
                [self.cityCV reloadData];
            }
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"请求失败");
    }];
}


@end
